package com.xbongbong.paas.controller;

import com.alibaba.dingtalk.openapi.newmessage.entity.ActionCard;
import com.alibaba.dingtalk.openapi.newmessage.entity.File;
import com.alibaba.dingtalk.openapi.newmessage.entity.Image;
import com.alibaba.dingtalk.openapi.newmessage.entity.Link;
import com.alibaba.dingtalk.openapi.newmessage.entity.Markdown;
import com.alibaba.dingtalk.openapi.newmessage.entity.OA;
import com.alibaba.dingtalk.openapi.newmessage.entity.Text;
import com.alibaba.dingtalk.openapi.newmessage.entity.Voice;
import com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper;
import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageSendTestDTO;
import com.xbongbong.pro.message.service.MessageNewService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 消息 钉钉新消息接口测试用, 测试后会删除
 * @author 魏荣杰
 * @date 2019/3/22 15:57
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/test")
public class MessageTestController {

    @Resource
    private MessageNewService messageNewService;
    @Resource
    private ProBaseConfig proBaseConfig;

    @RequestMapping(value = "/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String send(@RequestBody @Valid MessageSendTestDTO messageSendTestDTO, BindingResult br) throws Exception{
        XbbResponse<CorpConversationHelper.SendResponse> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            if (!Objects.equals(proBaseConfig.getXbbCode(), messageSendTestDTO.getXbbCode())) {
                throw new XbbException(ErrorCodeEnum.API_FAIL);
            }
            CorpConversationHelper.SendResponse sendResponse = new CorpConversationHelper.SendResponse();
            try {
                switch (messageSendTestDTO.getMsgType()) {
                    case "action_card":
                        ActionCard actionCard = messageSendTestDTO.getActionCard();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), actionCard, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    case "file":
                        File file = messageSendTestDTO.getFile();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), file, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    case "image":
                        Image image = messageSendTestDTO.getImage();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), image, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    case "link":
                        Link link = messageSendTestDTO.getLink();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), link, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    case "markdown":
                        Markdown markdown = messageSendTestDTO.getMarkdown();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), markdown, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    case "oa":
                        OA oa = messageSendTestDTO.getOa();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), oa, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    case "text":
                        Text text = messageSendTestDTO.getText();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), text, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    case "voice":
                        Voice voice = messageSendTestDTO.getVoice();
                        sendResponse = messageNewService.dingtalkOAMessagePushToUserAndDept(messageSendTestDTO.getCorpid(), voice, messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                        break;
                    default:
                        break;
                }
                response = new XbbResponse<>(sendResponse);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/progress", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String progress(@RequestBody @Valid MessageSendTestDTO messageSendTestDTO, BindingResult br) throws Exception{
        XbbResponse<CorpConversationHelper.ProgressResponse> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (!Objects.equals(proBaseConfig.getXbbCode(), messageSendTestDTO.getXbbCode())) {
                    throw new XbbException(ErrorCodeEnum.API_FAIL);
                }
                CorpConversationHelper.ProgressResponse progressResponse = messageNewService.dingtalkOAMessagePushProgress(messageSendTestDTO.getCorpid(), messageSendTestDTO.getTaskId());
                response = new XbbResponse<>(progressResponse);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/sendResult", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendResult(@RequestBody @Valid MessageSendTestDTO messageSendTestDTO, BindingResult br) throws Exception{
        XbbResponse<CorpConversationHelper.SendResultResponse> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (!Objects.equals(proBaseConfig.getXbbCode(), messageSendTestDTO.getXbbCode())) {
                    throw new XbbException(ErrorCodeEnum.API_FAIL);
                }
                CorpConversationHelper.SendResultResponse sendResultResponse = messageNewService.dingtalkOAMessagePushSendResult(messageSendTestDTO.getCorpid(), messageSendTestDTO.getTaskId());
                response = new XbbResponse<>(sendResultResponse);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/sendByTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendByTemplate(@RequestBody @Valid MessageSendTestDTO messageSendTestDTO, BindingResult br) throws Exception{
        XbbResponse<CorpConversationHelper.SendByTemplateResponse> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            if (!Objects.equals(proBaseConfig.getXbbCode(), messageSendTestDTO.getXbbCode())) {
                throw new XbbException(ErrorCodeEnum.API_FAIL);
            }
            CorpConversationHelper.SendByTemplateResponse sendResponse;
            try {
                sendResponse = messageNewService.dingtalkWorkMessageByTemplatePushToUserAndDept(messageSendTestDTO.getCorpid(), messageSendTestDTO.getTemplateMessage(), messageSendTestDTO.getUserIdList(), messageSendTestDTO.getDepIdList());
                response = new XbbResponse<>(sendResponse);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
